﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using Infrasturcture.Global.Controls.Dialog;
using Infrasturcture.Global.Helpers.CustomHelpers.Events;
using Infrasturcture.TouchLibrary;
using MallInteractiveMap.DTO;
using MallInteractiveMap.Language;
using System.Linq;

namespace CustomControls.SearchResult
{
    /// <summary>
    /// Interaction logic for ShopSearchResult.xaml
    /// </summary>
    public partial class ShopSearchResult : ADialogContent, IMTouchControl
    {
        private readonly List<DTOShop> _shops;
        public IMTContainer Container { get; set; }
        public List<ShopResultItem> ShopResultItems =new List<ShopResultItem>();
        public ScrollViewer ScrollViewer
        {
            get
            {
                return Scroll;
            }
        }
        #region DependencyProperties


        public string SearchResultText
        {
            get { return (string)GetValue(SearchResultTextProperty); }
            set { SetValue(SearchResultTextProperty, value); }
        }

        public static readonly DependencyProperty SearchResultTextProperty =
            DependencyProperty.Register("SearchResultText", typeof(string), typeof(ShopSearchResult), new UIPropertyMetadata(LanguageLoader.GetText("Search Result")));



        public string ShowText
        {
            get { return (string)GetValue(ShowTextProperty); }
            set { SetValue(ShowTextProperty, value); }
        }

        public static readonly DependencyProperty ShowTextProperty =
            DependencyProperty.Register("ShowText", typeof(string), typeof(ShopSearchResult), new UIPropertyMetadata(LanguageLoader.GetText("Show")));



        public string FloorText
        {
            get { return (string)GetValue(FloorTextProperty); }
            set { SetValue(FloorTextProperty, value); }
        }

        public static readonly DependencyProperty FloorTextProperty =
            DependencyProperty.Register("FloorText", typeof(string), typeof(ShopSearchResult), new UIPropertyMetadata(LanguageLoader.GetText("FLOOR")));



        public string DetailsText
        {
            get { return (string)GetValue(DetailsTextProperty); }
            set { SetValue(DetailsTextProperty, value); }
        }

        public static readonly DependencyProperty DetailsTextProperty =
            DependencyProperty.Register("DetailsText", typeof(string), typeof(ShopSearchResult), new UIPropertyMetadata(LanguageLoader.GetText("Details")));



        public string CategoryText
        {
            get { return (string)GetValue(CategoryTextProperty); }
            set { SetValue(CategoryTextProperty, value); }
        }

        public static readonly DependencyProperty CategoryTextProperty =
            DependencyProperty.Register("CategoryText", typeof(string), typeof(ShopSearchResult), new UIPropertyMetadata(LanguageLoader.GetText("CATEGORY")));



        public string ShopNoText
        {
            get { return (string)GetValue(ShopNoTextProperty); }
            set { SetValue(ShopNoTextProperty, value); }
        }

        public static readonly DependencyProperty ShopNoTextProperty =
            DependencyProperty.Register("ShopNoText", typeof(string), typeof(ShopSearchResult), new UIPropertyMetadata(LanguageLoader.GetText("Shop No.")));



        #endregion

        public ShopSearchResult(List<DTOShop> shops)
        {
            DataContext = this;
            _shops = shops.Take(shops.Count>20?20:shops.Count).ToList();
            InitializeComponent();
            LoadResult();
            LoadTexts();
        }

        private void LoadTexts()
        {
            SearchResultText = LanguageLoader.GetText("Search Result");
            ShowText = LanguageLoader.GetText("Show");
            FloorText = LanguageLoader.GetText("FLOOR");
            CategoryText = LanguageLoader.GetText("CATEGORY");
            ShopNoText = LanguageLoader.GetText("Shop No.");
            DetailsText = LanguageLoader.GetText("Details");
        }

        private void LoadResult()
        {
            foreach (var shop in _shops)
            {
                var shopResultItem = new ShopResultItem(shop.Id, shop.Title, shop.Description, shop.ShopNo,shop.FloorName, shop.CategoryName);
                ItemContainer.Children.Add(shopResultItem);
                ShopResultItems.Add(shopResultItem);
                shopResultItem.Selected += ShopSelected;
            }
        }

        void ShopSelected(object sender, EventArgs eventArgs)
        {
            OnClosed(new DataEventArgs() {Data = sender});
        }
    }
}
